package hk.ust.cse.comp3111.studentSchedule;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Fragment;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;


public class AddExample extends Fragment implements OnClickListener{
	
	public AddExample(){}
	
	Button insertButton;
	Button insertButton2;
	EditText title;
	EditText description;
	DatePicker fromDate, toDate;
	TimePicker fromTime, toTime;
	TextView textssssss;
	long calendarID;
	long eventID;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.add_example, container, false);
         
        return rootView;
    }
	
	
	
	@Override
	public void onStart() {
		super.onStart();
		//Button insertButton = (Button)this.getView().findViewById(R.id.buttonTest1);
    	//TextView text1=(TextView)this.getView().findViewById(R.id.textTest1);
        insertButton = (Button)this.getView().findViewById(R.id.buttonInsertEvent);
        insertButton.setOnClickListener(this);
        insertButton2 = (Button)this.getView().findViewById(R.id.buttonInsertEvent2);
        insertButton2.setOnClickListener(this);
        textssssss = (TextView)this.getView().findViewById(R.id.text1);
        
        
        title=(EditText)this.getView().findViewById(R.id.titleInput);
        description=(EditText)this.getView().findViewById(R.id.descriptionInput);
        fromDate=(DatePicker)this.getView().findViewById(R.id.fromDate);
        toDate=(DatePicker)this.getView().findViewById(R.id.toDate);;
        fromTime=(TimePicker)this.getView().findViewById(R.id.fromTime);;
        toTime=(TimePicker)this.getView().findViewById(R.id.toTime);;	
	
	
	}
	
	
	


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
        case R.id.buttonInsertEvent:
        	addEvent();
            break;
        case R.id.buttonInsertEvent2:
        	textssssss.setText("The event is "+title.getText().toString()+" with description "+ description.getText().toString() +" from " + getTime(fromDate,fromTime) + " to " + getTime(toDate,toTime));
        	break;
        }   
		
	}
	
	private void addEvent() {
    	calendarID = getCalendarID();	
    	ContentValues eventValues = new ContentValues ();
    	// provide the required fields for creating an event to
    	// ContentValues instance and insert event using
    	// ContentResolver
    	eventValues.put(CalendarContract.Events.CALENDAR_ID,calendarID);
    	eventValues.put (CalendarContract.Events.TITLE,title.getText().toString());
    	eventValues.put (CalendarContract.Events.DESCRIPTION,description.getText().toString());
    	eventValues.put(CalendarContract.Events.DTSTART,getTime(fromDate,fromTime));
    	eventValues.put(CalendarContract.Events.DTEND,getTime(toDate,toTime));
    	eventValues.put(CalendarContract.Events.EVENT_TIMEZONE,TimeZone.getDefault().toString());
    	Uri eventUri = this.getActivity().getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues);
    	eventID = ContentUris.parseId(eventUri);
    	textssssss.setText("event id "+ eventID +" inserted");
    }
 // we use this method in order to get the ID of the calendar because
 // calendar ID is a required field in creating an event
    
    public long getCalendarID() {
    	Cursor cur = null;
    	try {
    	// provide CalendarContract.Calendars.CONTENT_URI to
    	// ContentResolver to query calendars
    		cur = this.getActivity().getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,null,null,null, null);
    		if (cur.moveToFirst()) {
    			return cur.getLong(cur.getColumnIndex(CalendarContract.Calendars._ID));
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		} finally {
		    	if (cur != null) {
		    	cur.close();
		    	}
    		}
    	return -1L;
    	}
    	
    
    
    long getTime(DatePicker date, TimePicker time){
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(date.getYear(), date.getMonth(), date.getDayOfMonth(), 
    			time.getCurrentHour(), time.getCurrentMinute(), 0);
    	long startTime = calendar.getTimeInMillis();
    	return startTime;
    }
	
	
	
}
